package com.sun.sangeng;

import java.util.function.Function;
import java.util.function.IntBinaryOperator;
import java.util.function.IntConsumer;
import java.util.function.IntPredicate;

public class LambdaDemo01 {

    public static void main(String[] args) {

        //threadTest();

        //test2_1();
        //test2_2();
        //test2_3();
        //test2_4();
        //test2_5();

        //test3_1();
        //test3_2();
        //test3_3();

        //test4_1();
        //test4_2();
        //test4_3();
        //test4_4();

        test5_1();
        test5_2();
    }

    private static void test5_2() {
        Integer[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        foreachArr(arr,i -> System.out.print(i+1));
    }

    private static void test5_1() {
        Integer[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        foreachArr(arr, new IntConsumer() {
            @Override
            public void accept(int value) {
                System.out.println(value + 1);
            }
        });
    }

    private static void foreachArr(Integer[] arr, IntConsumer consumer) {
        for (int i : arr) {
            consumer.accept(i);
        }
    }

    private static void test4_4() {
        Integer integer = typeConvert_1("123", Integer::valueOf);
        System.out.println(integer);
    }

    private static void test4_3() {
        Integer result = typeConvert(strNumber -> Integer.valueOf(strNumber));
        System.out.println(result);
    }

    private static void test4_2() {
        Integer integer = typeConvert((String strNumber) -> {
            return Integer.valueOf(strNumber);
        });
        System.out.println(integer);
    }

    private static void test4_1() {
        Integer result = typeConvert(new Function<String, Integer>() {
            @Override
            public Integer apply(String s) {
                return Integer.parseInt(s);
            }
        });
        System.out.println(result);
    }

    public static <R> R typeConvert_1(String strNumber, Function<String, R> function) {
        R result = function.apply(strNumber);
        return result;
    }

    public static <R> R typeConvert(Function<String, R> function) {
        String str = "1235";
        R result = function.apply(str);
        return result;
    }

    private static void test3_3() {
        printNum(i -> i % 2 == 0);
    }

    private static void test3_2() {
        printNum((int i) -> {
            return i % 2 == 0;
        });
    }

    private static void test3_1() {
        printNum(new IntPredicate() {
            @Override
            public boolean test(int value) {
                return value % 2 == 0;
            }
        });
    }

    public static void printNum(IntPredicate predicate) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        for (int i : arr) {
            if (predicate.test(i)) {
                System.out.println(i);
            }
        }
    }

    private static void test2_5() {
        int i = calculateNum(Integer::sum);
        System.out.println(i);
    }

    private static void test2_4() {
        int i = calculateNum((a, b) -> a + b);
        System.out.println(i);
    }

    private static void test2_3() {
        int i = calculateNum((int a, int b) -> a + b);
        System.out.println(i);
    }

    private static void test2_2() {
        int i = calculateNum((int a, int b) -> {
            return a + b;
        });
        System.out.println(i);
    }

    private static void test2_1() {
        int i = calculateNum(new IntBinaryOperator() {
            @Override
            public int applyAsInt(int left, int right) {
                return left + right;
            }
        });
        System.out.println(i);
    }

    public static int calculateNum(IntBinaryOperator operator) {
        int a = 10;
        int b = 20;
        return operator.applyAsInt(a, b);
    }

    private static void threadTest() {
        // 1. 创建线程：匿名内部类的写法
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("新线程中的方法被执行了");
            }
        }).start();

        // 2. 创建线程：Lambda表达式的写法
        new Thread(() -> {
            System.out.println("新线程中的方法被执行了");
        }).start();

        // 3. 创建线程：Lambda表达式的简化写法
        new Thread(() -> System.out.println("新线程中的方法被执行了")).start();
    }
}
